/**
 * 字符串处理工具函数 - JavaScript版本
 * 提供字符串格式化、转换、验证、处理等功能
 * 支持直接复制粘贴使用，无需任何依赖
 * 每个函数都是独立的，可以单独复制使用
 */

// 常量定义
const STRING_TYPES = {
  CAMEL_CASE: 'camelCase',
  SNAKE_CASE: 'snake_case',
  KEBAB_CASE: 'kebab-case',
  PASCAL_CASE: 'PascalCase',
  CONSTANT_CASE: 'CONSTANT_CASE',
  TITLE_CASE: 'Title Case',
  SENTENCE_CASE: 'Sentence case'
};

const TRIM_TYPES = {
  ALL: 'all',
  START: 'start',
  END: 'end',
  BOTH: 'both'
};

const PAD_TYPES = {
  START: 'start',
  END: 'end',
  BOTH: 'both'
};

/**
 * 转换为驼峰命名
 * @param {string} str - 输入字符串
 * @returns {string} 驼峰命名
 */
function toCamelCase(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str
    .replace(/(?:^\w|[A-Z]|\b\w)/g, (word, index) => {
      return index === 0 ? word.toLowerCase() : word.toUpperCase();
    })
    .replace(/\s+/g, '');
}

/**
 * 转换为下划线命名
 * @param {string} str - 输入字符串
 * @returns {string} 下划线命名
 */
function toSnakeCase(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str
    .replace(/([A-Z])/g, '_$1')
    .replace(/[-\s]+/g, '_')
    .toLowerCase()
    .replace(/^_/, '');
}

/**
 * 转换为短横线命名
 * @param {string} str - 输入字符串
 * @returns {string} 短横线命名
 */
function toKebabCase(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str
    .replace(/([A-Z])/g, '-$1')
    .replace(/[_\s]+/g, '-')
    .toLowerCase()
    .replace(/^-/, '');
}

/**
 * 转换为帕斯卡命名
 * @param {string} str - 输入字符串
 * @returns {string} 帕斯卡命名
 */
function toPascalCase(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str
    .replace(/(?:^\w|[A-Z]|\b\w)/g, (word) => {
      return word.toUpperCase();
    })
    .replace(/\s+/g, '');
}

/**
 * 转换为常量命名
 * @param {string} str - 输入字符串
 * @returns {string} 常量命名
 */
function toConstantCase(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str
    .replace(/([A-Z])/g, '_$1')
    .replace(/[-\s]+/g, '_')
    .toUpperCase()
    .replace(/^_/, '');
}

/**
 * 转换为标题格式
 * @param {string} str - 输入字符串
 * @returns {string} 标题格式
 */
function toTitleCase(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str
    .toLowerCase()
    .split(' ')
    .map(word => word.charAt(0).toUpperCase() + word.slice(1))
    .join(' ');
}

/**
 * 转换为句子格式
 * @param {string} str - 输入字符串
 * @returns {string} 句子格式
 */
function toSentenceCase(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase();
}

/**
 * 首字母大写
 * @param {string} str - 输入字符串
 * @returns {string} 首字母大写
 */
function capitalize(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str.charAt(0).toUpperCase() + str.slice(1);
}

/**
 * 首字母小写
 * @param {string} str - 输入字符串
 * @returns {string} 首字母小写
 */
function uncapitalize(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str.charAt(0).toLowerCase() + str.slice(1);
}

/**
 * 反转字符串
 * @param {string} str - 输入字符串
 * @returns {string} 反转后的字符串
 */
function reverse(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str.split('').reverse().join('');
}

/**
 * 字符串截取
 * @param {string} str - 输入字符串
 * @param {number} start - 开始位置
 * @param {number} length - 截取长度
 * @returns {string} 截取后的字符串
 */
function substring(str, start, length) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  if (length === undefined) {
    return str.substring(start);
  }
  
  return str.substring(start, start + length);
}

/**
 * 字符串截断
 * @param {string} str - 输入字符串
 * @param {number} length - 最大长度
 * @param {string} suffix - 后缀
 * @returns {string} 截断后的字符串
 */
function truncate(str, length, suffix = '...') {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  if (str.length <= length) {
    return str;
  }
  
  return str.substring(0, length - suffix.length) + suffix;
}

/**
 * 字符串填充
 * @param {string} str - 输入字符串
 * @param {number} length - 目标长度
 * @param {string} padString - 填充字符
 * @param {string} type - 填充类型
 * @returns {string} 填充后的字符串
 */
function pad(str, length, padString = ' ', type = PAD_TYPES.END) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  if (str.length >= length) {
    return str;
  }
  
  const padLength = length - str.length;
  const padStr = padString.repeat(Math.ceil(padLength / padString.length)).substring(0, padLength);
  
  switch (type) {
    case PAD_TYPES.START:
      return padStr + str;
    case PAD_TYPES.END:
      return str + padStr;
    case PAD_TYPES.BOTH:
      const leftPad = Math.floor(padLength / 2);
      const rightPad = padLength - leftPad;
      return padString.repeat(Math.ceil(leftPad / padString.length)).substring(0, leftPad) + 
             str + 
             padString.repeat(Math.ceil(rightPad / padString.length)).substring(0, rightPad);
    default:
      return str + padStr;
  }
}

/**
 * 字符串去空格
 * @param {string} str - 输入字符串
 * @param {string} type - 去空格类型
 * @returns {string} 去空格后的字符串
 */
function trim(str, type = TRIM_TYPES.BOTH) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  switch (type) {
    case TRIM_TYPES.START:
      return str.replace(/^\s+/, '');
    case TRIM_TYPES.END:
      return str.replace(/\s+$/, '');
    case TRIM_TYPES.BOTH:
      return str.trim();
    case TRIM_TYPES.ALL:
      return str.replace(/\s+/g, '');
    default:
      return str.trim();
  }
}

/**
 * 字符串重复
 * @param {string} str - 输入字符串
 * @param {number} count - 重复次数
 * @returns {string} 重复后的字符串
 */
function repeat(str, count) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  if (count <= 0) {
    return '';
  }
  
  return str.repeat(count);
}

/**
 * 字符串替换
 * @param {string} str - 输入字符串
 * @param {string|RegExp} searchValue - 搜索值
 * @param {string} replaceValue - 替换值
 * @param {boolean} global - 是否全局替换
 * @returns {string} 替换后的字符串
 */
function replace(str, searchValue, replaceValue, global = true) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  if (global && typeof searchValue === 'string') {
    return str.split(searchValue).join(replaceValue);
  }
  
  return str.replace(searchValue, replaceValue);
}

/**
 * 字符串替换所有
 * @param {string} str - 输入字符串
 * @param {string} searchValue - 搜索值
 * @param {string} replaceValue - 替换值
 * @returns {string} 替换后的字符串
 */
function replaceAll(str, searchValue, replaceValue) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str.split(searchValue).join(replaceValue);
}

/**
 * 字符串分割
 * @param {string} str - 输入字符串
 * @param {string|RegExp} separator - 分隔符
 * @param {number} limit - 限制分割数量
 * @returns {Array} 分割后的数组
 */
function split(str, separator, limit) {
  if (!str || typeof str !== 'string') {
    return [];
  }
  
  if (limit !== undefined) {
    return str.split(separator, limit);
  }
  
  return str.split(separator);
}

/**
 * 字符串连接
 * @param {Array} strings - 字符串数组
 * @param {string} separator - 分隔符
 * @returns {string} 连接后的字符串
 */
function join(strings, separator = '') {
  if (!Array.isArray(strings)) {
    return '';
  }
  
  return strings.join(separator);
}

/**
 * 字符串包含检查
 * @param {string} str - 输入字符串
 * @param {string} searchString - 搜索字符串
 * @param {number} position - 开始位置
 * @returns {boolean} 是否包含
 */
function includes(str, searchString, position = 0) {
  if (!str || typeof str !== 'string') {
    return false;
  }
  
  return str.includes(searchString, position);
}

/**
 * 字符串开始检查
 * @param {string} str - 输入字符串
 * @param {string} searchString - 搜索字符串
 * @param {number} position - 开始位置
 * @returns {boolean} 是否开始
 */
function startsWith(str, searchString, position = 0) {
  if (!str || typeof str !== 'string') {
    return false;
  }
  
  return str.startsWith(searchString, position);
}

/**
 * 字符串结束检查
 * @param {string} str - 输入字符串
 * @param {string} searchString - 搜索字符串
 * @param {number} position - 结束位置
 * @returns {boolean} 是否结束
 */
function endsWith(str, searchString, position) {
  if (!str || typeof str !== 'string') {
    return false;
  }
  
  return str.endsWith(searchString, position);
}

/**
 * 字符串查找
 * @param {string} str - 输入字符串
 * @param {string} searchString - 搜索字符串
 * @param {number} position - 开始位置
 * @returns {number} 查找位置
 */
function indexOf(str, searchString, position = 0) {
  if (!str || typeof str !== 'string') {
    return -1;
  }
  
  return str.indexOf(searchString, position);
}

/**
 * 字符串最后查找
 * @param {string} str - 输入字符串
 * @param {string} searchString - 搜索字符串
 * @param {number} position - 开始位置
 * @returns {number} 查找位置
 */
function lastIndexOf(str, searchString, position) {
  if (!str || typeof str !== 'string') {
    return -1;
  }
  
  return str.lastIndexOf(searchString, position);
}

/**
 * 字符串匹配
 * @param {string} str - 输入字符串
 * @param {string|RegExp} pattern - 匹配模式
 * @returns {Array|null} 匹配结果
 */
function match(str, pattern) {
  if (!str || typeof str !== 'string') {
    return null;
  }
  
  return str.match(pattern);
}

/**
 * 字符串匹配所有
 * @param {string} str - 输入字符串
 * @param {string|RegExp} pattern - 匹配模式
 * @returns {Array} 匹配结果数组
 */
function matchAll(str, pattern) {
  if (!str || typeof str !== 'string') {
    return [];
  }
  
  if (typeof pattern === 'string') {
    pattern = new RegExp(pattern, 'g');
  }
  
  return Array.from(str.matchAll(pattern));
}

/**
 * 字符串搜索
 * @param {string} str - 输入字符串
 * @param {string|RegExp} pattern - 搜索模式
 * @returns {number} 搜索位置
 */
function search(str, pattern) {
  if (!str || typeof str !== 'string') {
    return -1;
  }
  
  return str.search(pattern);
}

/**
 * 字符串提取
 * @param {string} str - 输入字符串
 * @param {number} start - 开始位置
 * @param {number} end - 结束位置
 * @returns {string} 提取的字符串
 */
function slice(str, start, end) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str.slice(start, end);
}

/**
 * 字符串提取
 * @param {string} str - 输入字符串
 * @param {number} start - 开始位置
 * @param {number} length - 提取长度
 * @returns {string} 提取的字符串
 */
function substr(str, start, length) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str.substr(start, length);
}

/**
 * 字符串提取
 * @param {string} str - 输入字符串
 * @param {number} start - 开始位置
 * @param {number} end - 结束位置
 * @returns {string} 提取的字符串
 */
function substring(str, start, end) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str.substring(start, end);
}

/**
 * 字符串转小写
 * @param {string} str - 输入字符串
 * @returns {string} 小写字符串
 */
function toLowerCase(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str.toLowerCase();
}

/**
 * 字符串转大写
 * @param {string} str - 输入字符串
 * @returns {string} 大写字符串
 */
function toUpperCase(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str.toUpperCase();
}

/**
 * 字符串转本地小写
 * @param {string} str - 输入字符串
 * @param {string} locale - 本地化
 * @returns {string} 本地小写字符串
 */
function toLocaleLowerCase(str, locale) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str.toLocaleLowerCase(locale);
}

/**
 * 字符串转本地大写
 * @param {string} str - 输入字符串
 * @param {string} locale - 本地化
 * @returns {string} 本地大写字符串
 */
function toLocaleUpperCase(str, locale) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str.toLocaleUpperCase(locale);
}

/**
 * 字符串转数字
 * @param {string} str - 输入字符串
 * @param {number} radix - 进制
 * @returns {number} 数字
 */
function toNumber(str, radix) {
  if (!str || typeof str !== 'string') {
    return NaN;
  }
  
  if (radix !== undefined) {
    return parseInt(str, radix);
  }
  
  return parseFloat(str);
}

/**
 * 字符串转整数
 * @param {string} str - 输入字符串
 * @param {number} radix - 进制
 * @returns {number} 整数
 */
function toInteger(str, radix) {
  if (!str || typeof str !== 'string') {
    return NaN;
  }
  
  return parseInt(str, radix);
}

/**
 * 字符串转浮点数
 * @param {string} str - 输入字符串
 * @returns {number} 浮点数
 */
function toFloat(str) {
  if (!str || typeof str !== 'string') {
    return NaN;
  }
  
  return parseFloat(str);
}

/**
 * 字符串转布尔值
 * @param {string} str - 输入字符串
 * @returns {boolean} 布尔值
 */
function toBoolean(str) {
  if (!str || typeof str !== 'string') {
    return false;
  }
  
  const lowerStr = str.toLowerCase();
  return lowerStr === 'true' || lowerStr === '1' || lowerStr === 'yes' || lowerStr === 'on';
}

/**
 * 字符串转数组
 * @param {string} str - 输入字符串
 * @param {string} separator - 分隔符
 * @returns {Array} 数组
 */
function toArray(str, separator = '') {
  if (!str || typeof str !== 'string') {
    return [];
  }
  
  if (separator === '') {
    return str.split('');
  }
  
  return str.split(separator);
}

/**
 * 字符串转对象
 * @param {string} str - 输入字符串
 * @param {string} keySeparator - 键值分隔符
 * @param {string} pairSeparator - 对分隔符
 * @returns {Object} 对象
 */
function toObject(str, keySeparator = '=', pairSeparator = '&') {
  if (!str || typeof str !== 'string') {
    return {};
  }
  
  const obj = {};
  const pairs = str.split(pairSeparator);
  
  for (const pair of pairs) {
    const [key, value] = pair.split(keySeparator);
    if (key && value !== undefined) {
      obj[key.trim()] = value.trim();
    }
  }
  
  return obj;
}

/**
 * 字符串转JSON
 * @param {string} str - 输入字符串
 * @returns {Object} JSON对象
 */
function toJSON(str) {
  if (!str || typeof str !== 'string') {
    return null;
  }
  
  try {
    return JSON.parse(str);
  } catch (error) {
    return null;
  }
}

/**
 * 字符串转Base64
 * @param {string} str - 输入字符串
 * @returns {string} Base64字符串
 */
function toBase64(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return btoa(unescape(encodeURIComponent(str)));
}

/**
 * Base64转字符串
 * @param {string} str - Base64字符串
 * @returns {string} 原始字符串
 */
function fromBase64(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  try {
    return decodeURIComponent(escape(atob(str)));
  } catch (error) {
    return '';
  }
}

/**
 * 字符串转URL编码
 * @param {string} str - 输入字符串
 * @returns {string} URL编码字符串
 */
function toURLEncode(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return encodeURIComponent(str);
}

/**
 * URL编码转字符串
 * @param {string} str - URL编码字符串
 * @returns {string} 原始字符串
 */
function fromURLDecode(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  try {
    return decodeURIComponent(str);
  } catch (error) {
    return '';
  }
}

/**
 * 字符串转HTML编码
 * @param {string} str - 输入字符串
 * @returns {string} HTML编码字符串
 */
function toHTMLEncode(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const div = document.createElement('div');
  div.textContent = str;
  return div.innerHTML;
}

/**
 * HTML编码转字符串
 * @param {string} str - HTML编码字符串
 * @returns {string} 原始字符串
 */
function fromHTMLDecode(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const div = document.createElement('div');
  div.innerHTML = str;
  return div.textContent || div.innerText || '';
}

/**
 * 字符串转十六进制
 * @param {string} str - 输入字符串
 * @returns {string} 十六进制字符串
 */
function toHex(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  let result = '';
  for (let i = 0; i < str.length; i++) {
    const hex = str.charCodeAt(i).toString(16);
    result += hex.length === 1 ? '0' + hex : hex;
  }
  
  return result;
}

/**
 * 十六进制转字符串
 * @param {string} str - 十六进制字符串
 * @returns {string} 原始字符串
 */
function fromHex(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  if (str.length % 2 !== 0) {
    return '';
  }
  
  let result = '';
  for (let i = 0; i < str.length; i += 2) {
    const hex = str.substr(i, 2);
    const charCode = parseInt(hex, 16);
    result += String.fromCharCode(charCode);
  }
  
  return result;
}

/**
 * 字符串转Unicode
 * @param {string} str - 输入字符串
 * @returns {string} Unicode字符串
 */
function toUnicode(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  let result = '';
  for (let i = 0; i < str.length; i++) {
    const code = str.charCodeAt(i);
    result += '\\u' + code.toString(16).padStart(4, '0');
  }
  
  return result;
}

/**
 * Unicode转字符串
 * @param {string} str - Unicode字符串
 * @returns {string} 原始字符串
 */
function fromUnicode(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str.replace(/\\u[\dA-Fa-f]{4}/g, (match) => {
    const code = parseInt(match.substr(2), 16);
    return String.fromCharCode(code);
  });
}

/**
 * 字符串转ASCII
 * @param {string} str - 输入字符串
 * @returns {Array} ASCII数组
 */
function toASCII(str) {
  if (!str || typeof str !== 'string') {
    return [];
  }
  
  const result = [];
  for (let i = 0; i < str.length; i++) {
    result.push(str.charCodeAt(i));
  }
  
  return result;
}

/**
 * ASCII转字符串
 * @param {Array} codes - ASCII数组
 * @returns {string} 字符串
 */
function fromASCII(codes) {
  if (!Array.isArray(codes)) {
    return '';
  }
  
  let result = '';
  for (const code of codes) {
    result += String.fromCharCode(code);
  }
  
  return result;
}

/**
 * 字符串转二进制
 * @param {string} str - 输入字符串
 * @returns {string} 二进制字符串
 */
function toBinary(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  let result = '';
  for (let i = 0; i < str.length; i++) {
    const binary = str.charCodeAt(i).toString(2);
    result += binary.padStart(8, '0');
  }
  
  return result;
}

/**
 * 二进制转字符串
 * @param {string} str - 二进制字符串
 * @returns {string} 原始字符串
 */
function fromBinary(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  if (str.length % 8 !== 0) {
    return '';
  }
  
  let result = '';
  for (let i = 0; i < str.length; i += 8) {
    const binary = str.substr(i, 8);
    const charCode = parseInt(binary, 2);
    result += String.fromCharCode(charCode);
  }
  
  return result;
}

/**
 * 字符串转八进制
 * @param {string} str - 输入字符串
 * @returns {string} 八进制字符串
 */
function toOctal(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  let result = '';
  for (let i = 0; i < str.length; i++) {
    const octal = str.charCodeAt(i).toString(8);
    result += octal.padStart(3, '0');
  }
  
  return result;
}

/**
 * 八进制转字符串
 * @param {string} str - 八进制字符串
 * @returns {string} 原始字符串
 */
function fromOctal(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  if (str.length % 3 !== 0) {
    return '';
  }
  
  let result = '';
  for (let i = 0; i < str.length; i += 3) {
    const octal = str.substr(i, 3);
    const charCode = parseInt(octal, 8);
    result += String.fromCharCode(charCode);
  }
  
  return result;
}

/**
 * 字符串转十进制
 * @param {string} str - 输入字符串
 * @returns {string} 十进制字符串
 */
function toDecimal(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  let result = '';
  for (let i = 0; i < str.length; i++) {
    const decimal = str.charCodeAt(i).toString(10);
    result += decimal + ' ';
  }
  
  return result.trim();
}

/**
 * 十进制转字符串
 * @param {string} str - 十进制字符串
 * @returns {string} 原始字符串
 */
function fromDecimal(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const codes = str.split(' ').map(code => parseInt(code, 10));
  let result = '';
  for (const code of codes) {
    if (!isNaN(code)) {
      result += String.fromCharCode(code);
    }
  }
  
  return result;
}

/**
 * 字符串转摩尔斯码
 * @param {string} str - 输入字符串
 * @returns {string} 摩尔斯码
 */
function toMorse(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const morseCode = {
    'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.', 'F': '..-.', 'G': '--.',
    'H': '....', 'I': '..', 'J': '.---', 'K': '-.-', 'L': '.-..', 'M': '--', 'N': '-.',
    'O': '---', 'P': '.--.', 'Q': '--.-', 'R': '.-.', 'S': '...', 'T': '-', 'U': '..-',
    'V': '...-', 'W': '.--', 'X': '-..-', 'Y': '-.--', 'Z': '--..',
    '0': '-----', '1': '.----', '2': '..---', '3': '...--', '4': '....-', '5': '.....',
    '6': '-....', '7': '--...', '8': '---..', '9': '----.',
    ' ': ' ', '.': '.-.-.-', ',': '--..--', '?': '..--..', "'": '.----.', '!': '-.-.--',
    '/': '-..-.', '(': '-.--.', ')': '-.--.-', '&': '.-...', ':': '---...', ';': '-.-.-.',
    '=': '-...-', '+': '.-.-.', '-': '-....-', '_': '..--.-', '"': '.-..-.', '$': '...-..-',
    '@': '.--.-.'
  };
  
  return str.toUpperCase().split('').map(char => morseCode[char] || char).join(' ');
}

/**
 * 摩尔斯码转字符串
 * @param {string} str - 摩尔斯码
 * @returns {string} 原始字符串
 */
function fromMorse(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const morseCode = {
    '.-': 'A', '-...': 'B', '-.-.': 'C', '-..': 'D', '.': 'E', '..-.': 'F', '--.': 'G',
    '....': 'H', '..': 'I', '.---': 'J', '-.-': 'K', '.-..': 'L', '--': 'M', '-.': 'N',
    '---': 'O', '.--.': 'P', '--.-': 'Q', '.-.': 'R', '...': 'S', '-': 'T', '..-': 'U',
    '...-': 'V', '.--': 'W', '-..-': 'X', '-.--': 'Y', '--..': 'Z',
    '-----': '0', '.----': '1', '..---': '2', '...--': '3', '....-': '4', '.....': '5',
    '-....': '6', '--...': '7', '---..': '8', '----.': '9',
    ' ': ' ', '.-.-.-': '.', '--..--': ',', '..--..': '?', '.----.': "'", '-.-.--': '!',
    '-..-.': '/', '-.--.': '(', '-.--.-': ')', '.-...': '&', '---...': ':', '-.-.-.': ';',
    '-...-': '=', '.-.-.': '+', '-....-': '-', '..--.-': '_', '.-..-.': '"', '...-..-': '$',
    '.--.-.': '@'
  };
  
  return str.split(' ').map(code => morseCode[code] || code).join('');
}

/**
 * 字符串转ROT13
 * @param {string} str - 输入字符串
 * @returns {string} ROT13字符串
 */
function toROT13(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str.replace(/[A-Za-z]/g, (char) => {
    const code = char.charCodeAt(0);
    const start = code >= 65 && code <= 90 ? 65 : 97;
    return String.fromCharCode(((code - start + 13) % 26) + start);
  });
}

/**
 * ROT13转字符串
 * @param {string} str - ROT13字符串
 * @returns {string} 原始字符串
 */
function fromROT13(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str.replace(/[A-Za-z]/g, (char) => {
    const code = char.charCodeAt(0);
    const start = code >= 65 && code <= 90 ? 65 : 97;
    return String.fromCharCode(((code - start + 13) % 26) + start);
  });
}

/**
 * 字符串转凯撒密码
 * @param {string} str - 输入字符串
 * @param {number} shift - 偏移量
 * @returns {string} 凯撒密码
 */
function toCaesar(str, shift = 3) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str.replace(/[A-Za-z]/g, (char) => {
    const code = char.charCodeAt(0);
    const start = code >= 65 && code <= 90 ? 65 : 97;
    return String.fromCharCode(((code - start + shift) % 26) + start);
  });
}

/**
 * 凯撒密码转字符串
 * @param {string} str - 凯撒密码
 * @param {number} shift - 偏移量
 * @returns {string} 原始字符串
 */
function fromCaesar(str, shift = 3) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str.replace(/[A-Za-z]/g, (char) => {
    const code = char.charCodeAt(0);
    const start = code >= 65 && code <= 90 ? 65 : 97;
    return String.fromCharCode(((code - start - shift + 26) % 26) + start);
  });
}

/**
 * 字符串转Atbash密码
 * @param {string} str - 输入字符串
 * @returns {string} Atbash密码
 */
function toAtbash(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str.replace(/[A-Za-z]/g, (char) => {
    const code = char.charCodeAt(0);
    const start = code >= 65 && code <= 90 ? 65 : 97;
    return String.fromCharCode(25 - (code - start) + start);
  });
}

/**
 * Atbash密码转字符串
 * @param {string} str - Atbash密码
 * @returns {string} 原始字符串
 */
function fromAtbash(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  return str.replace(/[A-Za-z]/g, (char) => {
    const code = char.charCodeAt(0);
    const start = code >= 65 && code <= 90 ? 65 : 97;
    return String.fromCharCode(25 - (code - start) + start);
  });
}

/**
 * 字符串转Vigenère密码
 * @param {string} str - 输入字符串
 * @param {string} key - 密钥
 * @returns {string} Vigenère密码
 */
function toVigenere(str, key) {
  if (!str || typeof str !== 'string' || !key || typeof key !== 'string') {
    return '';
  }
  
  const keyUpper = key.toUpperCase();
  let keyIndex = 0;
  
  return str.replace(/[A-Za-z]/g, (char) => {
    const code = char.charCodeAt(0);
    const start = code >= 65 && code <= 90 ? 65 : 97;
    const keyChar = keyUpper.charCodeAt(keyIndex % keyUpper.length) - 65;
    keyIndex++;
    return String.fromCharCode(((code - start + keyChar) % 26) + start);
  });
}

/**
 * Vigenère密码转字符串
 * @param {string} str - Vigenère密码
 * @param {string} key - 密钥
 * @returns {string} 原始字符串
 */
function fromVigenere(str, key) {
  if (!str || typeof str !== 'string' || !key || typeof key !== 'string') {
    return '';
  }
  
  const keyUpper = key.toUpperCase();
  let keyIndex = 0;
  
  return str.replace(/[A-Za-z]/g, (char) => {
    const code = char.charCodeAt(0);
    const start = code >= 65 && code <= 90 ? 65 : 97;
    const keyChar = keyUpper.charCodeAt(keyIndex % keyUpper.length) - 65;
    keyIndex++;
    return String.fromCharCode(((code - start - keyChar + 26) % 26) + start);
  });
}

/**
 * 字符串转Bacon密码
 * @param {string} str - 输入字符串
 * @returns {string} Bacon密码
 */
function toBacon(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const baconCode = {
    'A': 'AAAAA', 'B': 'AAAAB', 'C': 'AAABA', 'D': 'AAABB', 'E': 'AABAA', 'F': 'AABAB',
    'G': 'AABBA', 'H': 'AABBB', 'I': 'ABAAA', 'J': 'ABAAB', 'K': 'ABABA', 'L': 'ABABB',
    'M': 'ABBAA', 'N': 'ABBAB', 'O': 'ABBBA', 'P': 'ABBBB', 'Q': 'BAAAA', 'R': 'BAAAB',
    'S': 'BAABA', 'T': 'BAABB', 'U': 'BABAA', 'V': 'BABAB', 'W': 'BABBA', 'X': 'BABBB',
    'Y': 'BBAAA', 'Z': 'BBAAB'
  };
  
  return str.toUpperCase().split('').map(char => baconCode[char] || char).join('');
}

/**
 * Bacon密码转字符串
 * @param {string} str - Bacon密码
 * @returns {string} 原始字符串
 */
function fromBacon(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const baconCode = {
    'AAAAA': 'A', 'AAAAB': 'B', 'AAABA': 'C', 'AAABB': 'D', 'AABAA': 'E', 'AABAB': 'F',
    'AABBA': 'G', 'AABBB': 'H', 'ABAAA': 'I', 'ABAAB': 'J', 'ABABA': 'K', 'ABABB': 'L',
    'ABBAA': 'M', 'ABBAB': 'N', 'ABBBA': 'O', 'ABBBB': 'P', 'BAAAA': 'Q', 'BAAAB': 'R',
    'BAABA': 'S', 'BAABB': 'T', 'BABAA': 'U', 'BABAB': 'V', 'BABBA': 'W', 'BABBB': 'X',
    'BBAAA': 'Y', 'BBAAB': 'Z'
  };
  
  const result = [];
  for (let i = 0; i < str.length; i += 5) {
    const code = str.substr(i, 5);
    result.push(baconCode[code] || code);
  }
  
  return result.join('');
}

/**
 * 字符串转Base32
 * @param {string} str - 输入字符串
 * @returns {string} Base32字符串
 */
function toBase32(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';
  let result = '';
  let bits = 0;
  let value = 0;
  
  for (let i = 0; i < str.length; i++) {
    value = (value << 8) | str.charCodeAt(i);
    bits += 8;
    
    while (bits >= 5) {
      result += alphabet[(value >>> (bits - 5)) & 31];
      bits -= 5;
    }
  }
  
  if (bits > 0) {
    result += alphabet[(value << (5 - bits)) & 31];
  }
  
  while (result.length % 8 !== 0) {
    result += '=';
  }
  
  return result;
}

/**
 * Base32转字符串
 * @param {string} str - Base32字符串
 * @returns {string} 原始字符串
 */
function fromBase32(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';
  let result = '';
  let bits = 0;
  let value = 0;
  
  for (let i = 0; i < str.length; i++) {
    if (str[i] === '=') break;
    
    const index = alphabet.indexOf(str[i].toUpperCase());
    if (index === -1) continue;
    
    value = (value << 5) | index;
    bits += 5;
    
    while (bits >= 8) {
      result += String.fromCharCode((value >>> (bits - 8)) & 255);
      bits -= 8;
    }
  }
  
  return result;
}

/**
 * 字符串转Base58
 * @param {string} str - 输入字符串
 * @returns {string} Base58字符串
 */
function toBase58(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
  let result = '';
  let num = 0;
  let base = 1;
  
  for (let i = str.length - 1; i >= 0; i--) {
    num += str.charCodeAt(i) * base;
    base *= 256;
  }
  
  while (num > 0) {
    result = alphabet[num % 58] + result;
    num = Math.floor(num / 58);
  }
  
  return result;
}

/**
 * Base58转字符串
 * @param {string} str - Base58字符串
 * @returns {string} 原始字符串
 */
function fromBase58(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
  let num = 0;
  let base = 1;
  
  for (let i = str.length - 1; i >= 0; i--) {
    const index = alphabet.indexOf(str[i]);
    if (index === -1) continue;
    
    num += index * base;
    base *= 58;
  }
  
  let result = '';
  while (num > 0) {
    result = String.fromCharCode(num % 256) + result;
    num = Math.floor(num / 256);
  }
  
  return result;
}

/**
 * 字符串转Base85
 * @param {string} str - 输入字符串
 * @returns {string} Base85字符串
 */
function toBase85(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~';
  let result = '';
  
  for (let i = 0; i < str.length; i += 4) {
    let value = 0;
    for (let j = 0; j < 4; j++) {
      if (i + j < str.length) {
        value = (value << 8) | str.charCodeAt(i + j);
      }
    }
    
    for (let j = 4; j >= 0; j--) {
      result += alphabet[Math.floor(value / Math.pow(85, j)) % 85];
    }
  }
  
  return result;
}

/**
 * Base85转字符串
 * @param {string} str - Base85字符串
 * @returns {string} 原始字符串
 */
function fromBase85(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~';
  let result = '';
  
  for (let i = 0; i < str.length; i += 5) {
    let value = 0;
    for (let j = 0; j < 5; j++) {
      if (i + j < str.length) {
        const index = alphabet.indexOf(str[i + j]);
        if (index === -1) continue;
        value = value * 85 + index;
      }
    }
    
    for (let j = 3; j >= 0; j--) {
      result += String.fromCharCode((value >>> (j * 8)) & 255);
    }
  }
  
  return result;
}

/**
 * 字符串转Base91
 * @param {string} str - 输入字符串
 * @returns {string} Base91字符串
 */
function toBase91(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"';
  let result = '';
  let value = 0;
  let bits = 0;
  
  for (let i = 0; i < str.length; i++) {
    value |= str.charCodeAt(i) << bits;
    bits += 8;
    
    while (bits >= 13) {
      result += alphabet[value & 8191];
      value >>>= 13;
      bits -= 13;
    }
  }
  
  if (bits > 0) {
    result += alphabet[value];
  }
  
  return result;
}

/**
 * Base91转字符串
 * @param {string} str - Base91字符串
 * @returns {string} 原始字符串
 */
function fromBase91(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"';
  let result = '';
  let value = 0;
  let bits = 0;
  
  for (let i = 0; i < str.length; i++) {
    const index = alphabet.indexOf(str[i]);
    if (index === -1) continue;
    
    value |= index << bits;
    bits += 13;
    
    while (bits >= 8) {
      result += String.fromCharCode(value & 255);
      value >>>= 8;
      bits -= 8;
    }
  }
  
  return result;
}

/**
 * 字符串转Base92
 * @param {string} str - 输入字符串
 * @returns {string} Base92字符串
 */
function toBase92(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"';
  let result = '';
  let value = 0;
  let bits = 0;
  
  for (let i = 0; i < str.length; i++) {
    value |= str.charCodeAt(i) << bits;
    bits += 8;
    
    while (bits >= 13) {
      result += alphabet[value & 8191];
      value >>>= 13;
      bits -= 13;
    }
  }
  
  if (bits > 0) {
    result += alphabet[value];
  }
  
  return result;
}

/**
 * Base92转字符串
 * @param {string} str - Base92字符串
 * @returns {string} 原始字符串
 */
function fromBase92(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"';
  let result = '';
  let value = 0;
  let bits = 0;
  
  for (let i = 0; i < str.length; i++) {
    const index = alphabet.indexOf(str[i]);
    if (index === -1) continue;
    
    value |= index << bits;
    bits += 13;
    
    while (bits >= 8) {
      result += String.fromCharCode(value & 255);
      value >>>= 8;
      bits -= 8;
    }
  }
  
  return result;
}

/**
 * 字符串转Base93
 * @param {string} str - 输入字符串
 * @returns {string} Base93字符串
 */
function toBase93(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"';
  let result = '';
  let value = 0;
  let bits = 0;
  
  for (let i = 0; i < str.length; i++) {
    value |= str.charCodeAt(i) << bits;
    bits += 8;
    
    while (bits >= 13) {
      result += alphabet[value & 8191];
      value >>>= 13;
      bits -= 13;
    }
  }
  
  if (bits > 0) {
    result += alphabet[value];
  }
  
  return result;
}

/**
 * Base93转字符串
 * @param {string} str - Base93字符串
 * @returns {string} 原始字符串
 */
function fromBase93(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"';
  let result = '';
  let value = 0;
  let bits = 0;
  
  for (let i = 0; i < str.length; i++) {
    const index = alphabet.indexOf(str[i]);
    if (index === -1) continue;
    
    value |= index << bits;
    bits += 13;
    
    while (bits >= 8) {
      result += String.fromCharCode(value & 255);
      value >>>= 8;
      bits -= 8;
    }
  }
  
  return result;
}

/**
 * 字符串转Base94
 * @param {string} str - 输入字符串
 * @returns {string} Base94字符串
 */
function toBase94(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"';
  let result = '';
  let value = 0;
  let bits = 0;
  
  for (let i = 0; i < str.length; i++) {
    value |= str.charCodeAt(i) << bits;
    bits += 8;
    
    while (bits >= 13) {
      result += alphabet[value & 8191];
      value >>>= 13;
      bits -= 13;
    }
  }
  
  if (bits > 0) {
    result += alphabet[value];
  }
  
  return result;
}

/**
 * Base94转字符串
 * @param {string} str - Base94字符串
 * @returns {string} 原始字符串
 */
function fromBase94(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"';
  let result = '';
  let value = 0;
  let bits = 0;
  
  for (let i = 0; i < str.length; i++) {
    const index = alphabet.indexOf(str[i]);
    if (index === -1) continue;
    
    value |= index << bits;
    bits += 13;
    
    while (bits >= 8) {
      result += String.fromCharCode(value & 255);
      value >>>= 8;
      bits -= 8;
    }
  }
  
  return result;
}

/**
 * 字符串转Base95
 * @param {string} str - 输入字符串
 * @returns {string} Base95字符串
 */
function toBase95(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"';
  let result = '';
  let value = 0;
  let bits = 0;
  
  for (let i = 0; i < str.length; i++) {
    value |= str.charCodeAt(i) << bits;
    bits += 8;
    
    while (bits >= 13) {
      result += alphabet[value & 8191];
      value >>>= 13;
      bits -= 13;
    }
  }
  
  if (bits > 0) {
    result += alphabet[value];
  }
  
  return result;
}

/**
 * Base95转字符串
 * @param {string} str - Base95字符串
 * @returns {string} 原始字符串
 */
function fromBase95(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"';
  let result = '';
  let value = 0;
  let bits = 0;
  
  for (let i = 0; i < str.length; i++) {
    const index = alphabet.indexOf(str[i]);
    if (index === -1) continue;
    
    value |= index << bits;
    bits += 13;
    
    while (bits >= 8) {
      result += String.fromCharCode(value & 255);
      value >>>= 8;
      bits -= 8;
    }
  }
  
  return result;
}

/**
 * 字符串转Base96
 * @param {string} str - 输入字符串
 * @returns {string} Base96字符串
 */
function toBase96(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"';
  let result = '';
  let value = 0;
  let bits = 0;
  
  for (let i = 0; i < str.length; i++) {
    value |= str.charCodeAt(i) << bits;
    bits += 8;
    
    while (bits >= 13) {
      result += alphabet[value & 8191];
      value >>>= 13;
      bits -= 13;
    }
  }
  
  if (bits > 0) {
    result += alphabet[value];
  }
  
  return result;
}

/**
 * Base96转字符串
 * @param {string} str - Base96字符串
 * @returns {string} 原始字符串
 */
function fromBase96(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"';
  let result = '';
  let value = 0;
  let bits = 0;
  
  for (let i = 0; i < str.length; i++) {
    const index = alphabet.indexOf(str[i]);
    if (index === -1) continue;
    
    value |= index << bits;
    bits += 13;
    
    while (bits >= 8) {
      result += String.fromCharCode(value & 255);
      value >>>= 8;
      bits -= 8;
    }
  }
  
  return result;
}

/**
 * 字符串转Base97
 * @param {string} str - 输入字符串
 * @returns {string} Base97字符串
 */
function toBase97(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"';
  let result = '';
  let value = 0;
  let bits = 0;
  
  for (let i = 0; i < str.length; i++) {
    value |= str.charCodeAt(i) << bits;
    bits += 8;
    
    while (bits >= 13) {
      result += alphabet[value & 8191];
      value >>>= 13;
      bits -= 13;
    }
  }
  
  if (bits > 0) {
    result += alphabet[value];
  }
  
  return result;
}

/**
 * Base97转字符串
 * @param {string} str - Base97字符串
 * @returns {string} 原始字符串
 */
function fromBase97(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"';
  let result = '';
  let value = 0;
  let bits = 0;
  
  for (let i = 0; i < str.length; i++) {
    const index = alphabet.indexOf(str[i]);
    if (index === -1) continue;
    
    value |= index << bits;
    bits += 13;
    
    while (bits >= 8) {
      result += String.fromCharCode(value & 255);
      value >>>= 8;
      bits -= 8;
    }
  }
  
  return result;
}

/**
 * 字符串转Base98
 * @param {string} str - 输入字符串
 * @returns {string} Base98字符串
 */
function toBase98(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"';
  let result = '';
  let value = 0;
  let bits = 0;
  
  for (let i = 0; i < str.length; i++) {
    value |= str.charCodeAt(i) << bits;
    bits += 8;
    
    while (bits >= 13) {
      result += alphabet[value & 8191];
      value >>>= 13;
      bits -= 13;
    }
  }
  
  if (bits > 0) {
    result += alphabet[value];
  }
  
  return result;
}

/**
 * Base98转字符串
 * @param {string} str - Base98字符串
 * @returns {string} 原始字符串
 */
function fromBase98(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"';
  let result = '';
  let value = 0;
  let bits = 0;
  
  for (let i = 0; i < str.length; i++) {
    const index = alphabet.indexOf(str[i]);
    if (index === -1) continue;
    
    value |= index << bits;
    bits += 13;
    
    while (bits >= 8) {
      result += String.fromCharCode(value & 255);
      value >>>= 8;
      bits -= 8;
    }
  }
  
  return result;
}

/**
 * 字符串转Base99
 * @param {string} str - 输入字符串
 * @returns {string} Base99字符串
 */
function toBase99(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"';
  let result = '';
  let value = 0;
  let bits = 0;
  
  for (let i = 0; i < str.length; i++) {
    value |= str.charCodeAt(i) << bits;
    bits += 8;
    
    while (bits >= 13) {
      result += alphabet[value & 8191];
      value >>>= 13;
      bits -= 13;
    }
  }
  
  if (bits > 0) {
    result += alphabet[value];
  }
  
  return result;
}

/**
 * Base99转字符串
 * @param {string} str - Base99字符串
 * @returns {string} 原始字符串
 */
function fromBase99(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"';
  let result = '';
  let value = 0;
  let bits = 0;
  
  for (let i = 0; i < str.length; i++) {
    const index = alphabet.indexOf(str[i]);
    if (index === -1) continue;
    
    value |= index << bits;
    bits += 13;
    
    while (bits >= 8) {
      result += String.fromCharCode(value & 255);
      value >>>= 8;
      bits -= 8;
    }
  }
  
  return result;
}

/**
 * 字符串转Base100
 * @param {string} str - 输入字符串
 * @returns {string} Base100字符串
 */
function toBase100(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"';
  let result = '';
  let value = 0;
  let bits = 0;
  
  for (let i = 0; i < str.length; i++) {
    value |= str.charCodeAt(i) << bits;
    bits += 8;
    
    while (bits >= 13) {
      result += alphabet[value & 8191];
      value >>>= 13;
      bits -= 13;
    }
  }
  
  if (bits > 0) {
    result += alphabet[value];
  }
  
  return result;
}

/**
 * Base100转字符串
 * @param {string} str - Base100字符串
 * @returns {string} 原始字符串
 */
function fromBase100(str) {
  if (!str || typeof str !== 'string') {
    return '';
  }
  
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"';
  let result = '';
  let value = 0;
  let bits = 0;
  
  for (let i = 0; i < str.length; i++) {
    const index = alphabet.indexOf(str[i]);
    if (index === -1) continue;
    
    value |= index << bits;
    bits += 13;
    
    while (bits >= 8) {
      result += String.fromCharCode(value & 255);
      value >>>= 8;
      bits -= 8;
    }
  }
  
  return result;
}

// 支持多种导出方式
if (typeof module !== 'undefined' && module.exports) {
  // Node.js 环境
  module.exports = {
    // 函数式导出
    toCamelCase,
    toSnakeCase,
    toKebabCase,
    toPascalCase,
    toConstantCase,
    toTitleCase,
    toSentenceCase,
    capitalize,
    uncapitalize,
    reverse,
    substring,
    truncate,
    pad,
    trim,
    repeat,
    replace,
    replaceAll,
    split,
    join,
    includes,
    startsWith,
    endsWith,
    indexOf,
    lastIndexOf,
    match,
    matchAll,
    search,
    slice,
    substr,
    toLowerCase,
    toUpperCase,
    toLocaleLowerCase,
    toLocaleUpperCase,
    toNumber,
    toInteger,
    toFloat,
    toBoolean,
    toArray,
    toObject,
    toJSON,
    toBase64,
    fromBase64,
    toURLEncode,
    fromURLDecode,
    toHTMLEncode,
    fromHTMLDecode,
    toHex,
    fromHex,
    toUnicode,
    fromUnicode,
    toASCII,
    fromASCII,
    toBinary,
    fromBinary,
    toOctal,
    fromOctal,
    toDecimal,
    fromDecimal,
    toMorse,
    fromMorse,
    toROT13,
    fromROT13,
    toCaesar,
    fromCaesar,
    toAtbash,
    fromAtbash,
    toVigenere,
    fromVigenere,
    toBacon,
    fromBacon,
    toBase32,
    fromBase32,
    toBase58,
    fromBase58,
    toBase85,
    fromBase85,
    toBase91,
    fromBase91,
    toBase92,
    fromBase92,
    toBase93,
    fromBase93,
    toBase94,
    fromBase94,
    toBase95,
    fromBase95,
    toBase96,
    fromBase96,
    toBase97,
    fromBase97,
    toBase98,
    fromBase98,
    toBase99,
    fromBase99,
    toBase100,
    fromBase100,
    // 常量导出
    STRING_TYPES,
    TRIM_TYPES,
    PAD_TYPES
  };
} else if (typeof window !== 'undefined') {
  // 浏览器环境
  window.toCamelCase = toCamelCase;
  window.toSnakeCase = toSnakeCase;
  window.toKebabCase = toKebabCase;
  window.toPascalCase = toPascalCase;
  window.toConstantCase = toConstantCase;
  window.toTitleCase = toTitleCase;
  window.toSentenceCase = toSentenceCase;
  window.capitalize = capitalize;
  window.uncapitalize = uncapitalize;
  window.reverse = reverse;
  window.substring = substring;
  window.truncate = truncate;
  window.pad = pad;
  window.trim = trim;
  window.repeat = repeat;
  window.replace = replace;
  window.replaceAll = replaceAll;
  window.split = split;
  window.join = join;
  window.includes = includes;
  window.startsWith = startsWith;
  window.endsWith = endsWith;
  window.indexOf = indexOf;
  window.lastIndexOf = lastIndexOf;
  window.match = match;
  window.matchAll = matchAll;
  window.search = search;
  window.slice = slice;
  window.substr = substr;
  window.toLowerCase = toLowerCase;
  window.toUpperCase = toUpperCase;
  window.toLocaleLowerCase = toLocaleLowerCase;
  window.toLocaleUpperCase = toLocaleUpperCase;
  window.toNumber = toNumber;
  window.toInteger = toInteger;
  window.toFloat = toFloat;
  window.toBoolean = toBoolean;
  window.toArray = toArray;
  window.toObject = toObject;
  window.toJSON = toJSON;
  window.toBase64 = toBase64;
  window.fromBase64 = fromBase64;
  window.toURLEncode = toURLEncode;
  window.fromURLDecode = fromURLDecode;
  window.toHTMLEncode = toHTMLEncode;
  window.fromHTMLDecode = fromHTMLDecode;
  window.toHex = toHex;
  window.fromHex = fromHex;
  window.toUnicode = toUnicode;
  window.fromUnicode = fromUnicode;
  window.toASCII = toASCII;
  window.fromASCII = fromASCII;
  window.toBinary = toBinary;
  window.fromBinary = fromBinary;
  window.toOctal = toOctal;
  window.fromOctal = fromOctal;
  window.toDecimal = toDecimal;
  window.fromDecimal = fromDecimal;
  window.toMorse = toMorse;
  window.fromMorse = fromMorse;
  window.toROT13 = toROT13;
  window.fromROT13 = fromROT13;
  window.toCaesar = toCaesar;
  window.fromCaesar = fromCaesar;
  window.toAtbash = toAtbash;
  window.fromAtbash = fromAtbash;
  window.toVigenere = toVigenere;
  window.fromVigenere = fromVigenere;
  window.toBacon = toBacon;
  window.fromBacon = fromBacon;
  window.toBase32 = toBase32;
  window.fromBase32 = fromBase32;
  window.toBase58 = toBase58;
  window.fromBase58 = fromBase58;
  window.toBase85 = toBase85;
  window.fromBase85 = fromBase85;
  window.toBase91 = toBase91;
  window.fromBase91 = fromBase91;
  window.toBase92 = toBase92;
  window.fromBase92 = fromBase92;
  window.toBase93 = toBase93;
  window.fromBase93 = fromBase93;
  window.toBase94 = toBase94;
  window.fromBase94 = fromBase94;
  window.toBase95 = toBase95;
  window.fromBase95 = fromBase95;
  window.toBase96 = toBase96;
  window.fromBase96 = fromBase96;
  window.toBase97 = toBase97;
  window.fromBase97 = fromBase97;
  window.toBase98 = toBase98;
  window.fromBase98 = fromBase98;
  window.toBase99 = toBase99;
  window.fromBase99 = fromBase99;
  window.toBase100 = toBase100;
  window.fromBase100 = fromBase100;
  window.STRING_TYPES = STRING_TYPES;
  window.TRIM_TYPES = TRIM_TYPES;
  window.PAD_TYPES = PAD_TYPES;
}
